package com.zhudoo.zhilian.util;

import jodd.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Http工具类
 * Created by wangnian on 2016/6/2.
 */
public class HttpUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public static String get(String url) {
        long time = System.currentTimeMillis();
        String strResult = HttpRequest.get(url).send().bodyText();
        logger.info("url:{},response:{},耗时:{}", url, StringUtils.trim(strResult), System.currentTimeMillis() - time);
        return strResult;
    }

    /**
     * 带参数的get请求
     *
     * @param url
     * @param parMap
     * @return
     */
    public static String get(String url, Map<String, String> parMap) {
        long time = System.currentTimeMillis();
        HttpRequest httpRequest = HttpRequest.get(url);
        httpRequest.query(parMap);
        String strResult = httpRequest.send().bodyText();
        logger.info("url:{},parMap:{},response:{}，耗时:{}", url, httpRequest.query(), StringUtils.trim(strResult), System.currentTimeMillis() - time);
        return strResult;
    }

    /**
     * 带参数的Header请求的get请求
     *
     * @param url
     * @param parMap
     * @return
     */
    public static String get(String url, Map<String, String> parMap, Map<String, String> headerMap) {
        long time = System.currentTimeMillis();
        HttpRequest httpRequest = HttpRequest.get(url);
        httpRequest.query(parMap);
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            httpRequest.header(entry.getKey(), entry.getValue());
        }
        String strResult = httpRequest.send().bodyText();
        logger.info("url:{},parMap:{},response:{},耗时：{}", url, httpRequest.query(), StringUtils.trim(strResult), System.currentTimeMillis() - time);
        return strResult;
    }

    /**
     * 带参数post请求
     *
     * @param url
     * @return
     */
    public static String post(String url, Map<String, String> parMap) {
        long time = System.currentTimeMillis();
        HttpRequest httpRequest = HttpRequest.post(url);
        httpRequest.query(parMap);
        String strResult = httpRequest.send().bodyText();
        logger.info("url:{},parMap:{},response:{}，耗时:{}", url, httpRequest.query(), StringUtils.trim(strResult), System.currentTimeMillis() - time);
        return strResult;
    }


    /**
     * RAW Post请求
     *
     * @param url
     * @param body
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String post(String url, String body) {
        long time = System.currentTimeMillis();
        HttpRequest httpRequest = HttpRequest.post(url);
        httpRequest.bodyText(body);
        String strResult = httpRequest.send().bodyText();
        logger.info("url:{},Body:{},response:{},耗时:{}", url, body, StringUtils.trim(strResult), System.currentTimeMillis() - time);
        return strResult;
    }

}
